﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using IBuy.Entity;

namespace IBuy.Forms
{
    public partial class GoodsManageForm : Form
    {
        private Product Goods;
        private GoodType Type;
        private Company Company;

        private ConcreteObserver concreteObs;
        private ConcreteSubject concreteSub;
        public GoodsManageForm()
        {
            InitializeComponent();
            Goods = new Product();
            Type = new GoodType();
            Company = new Company();

            concreteSub = new ConcreteSubject();
            concreteSub.SubjectState = "开始";

            concreteObs = new ConcreteObserver(concreteSub, "系统",this.statusStrip);
            concreteSub.Attach(concreteObs);
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            concreteSub.SubjectState = "添加";
            concreteSub.Notify();
            this.statusStrip.Show();
            Hashtable table = new Hashtable();
            if (this.cmbType.SelectedValue.ToString().Equals("") ||
               this.txtName.Text.Equals("") ||
               this.txtPrice.Text.Equals("") ||
               this.txtStock.Text.Equals("") ||
               this.txtDelivery.Text.Equals(""))
            {
                MessageBox.Show("请确定所有项都不为空或者格式正确！");
                return;
            }
            table.Add("COMPANYID",this.cmbProduct.SelectedValue.ToString());
            table.Add("TYPEID", this.cmbType.SelectedValue.ToString());
            table.Add("NAME", this.txtName.Text);
            table.Add("PRICE", this.txtPrice.Text);
            table.Add("STOCK", this.txtStock.Text);
            table.Add("DELIVERY", this.txtDelivery.Text);
            Goods.InsertGoods(table);
            SetDataSource();
            ClearContentAll();
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            Hashtable table = new Hashtable();
            string where = "where NAME='"+this.txtName.Text+"'";
            if (this.cmbType.SelectedValue.ToString().Equals("") ||
               this.txtPrice.Text.Equals("") ||
               this.txtStock.Text.Equals("") ||
               this.txtDelivery.Text.Equals("")||
                this.cmbProduct.SelectedValue.ToString().Equals(""))
            {
                MessageBox.Show("请确定所有项都不为空或者格式正确！");
                return;
            }
            table.Add("COMPANYID", this.cmbProduct.SelectedValue.ToString());
            table.Add("TYPEID", this.cmbType.SelectedValue.ToString());
            table.Add("PRICE", this.txtPrice.Text);
            table.Add("STOCK", this.txtStock.Text);
            table.Add("DELIVERY", this.txtDelivery.Text);
            Goods.UpdateGoods(table, where);
            SetDataSource();
            ClearContentAll();
            concreteSub.SubjectState = "更新";
            concreteSub.Notify();
        }

        private void GoodsManageForm_Load(object sender, EventArgs e)
        {
            SetDataSource();
            this.cbKind.SelectedIndex = 0;
            BindTypeSource();
            concreteSub.Notify();
        }

        private void dgvGoods_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            DataGridViewRow row = this.dgvGoods.Rows[e.RowIndex];
            this.cmbProduct.Text = row.Cells["productCompany"].Value.ToString();
            this.cmbType.Text = row.Cells["GoodsType"].Value.ToString();
            this.txtName.Text = row.Cells["GoodsName"].Value.ToString();
            this.txtPrice.Text = row.Cells["Price"].Value.ToString();
            this.txtStock.Text = row.Cells["Stock"].Value.ToString();
            this.txtDelivery.Text = row.Cells["Delivery"].Value.ToString();
            concreteSub.SubjectState = "选择";
            concreteSub.Notify();
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            ClearContentAll();
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (this.txtName.Text.Equals(""))
            {
                MessageBox.Show("请确定待删除的物品名称不能为空！");
                return;
            }
            Goods.DeleteGoods(this.txtName.Text);
            SetDataSource();
            ClearContentAll();

            concreteSub.SubjectState = "删除";
            concreteSub.Notify();
        }

        private void btnQuery_Click(object sender, EventArgs e)
        {
            DataSet dataset = null;
            //如果选择查询“全部”
            if (this.cbKind.SelectedIndex.Equals(0))
            {
                dataset = Goods.GetAllGoods();
            }
            //如果根据“种类”进行查询
            if (this.cbKind.SelectedIndex.Equals(1))
            {
               dataset= Goods.GetGoodsByType(this.txtCondition.Text.Trim());
            }
            //如果根据“名称”进行查询
            if (this.cbKind.SelectedIndex.Equals(2))
            {
                dataset = Goods.GetGoodsByName(this.txtCondition.Text.Trim());
            }
            SetDataSource(dataset);
            ClearCondition();

            concreteSub.SubjectState = "查询";
            concreteSub.Notify();
        }

        private void SetDataSource()
        {
            DataSet dataset = Goods.GetAllGoods();
            this.dgvGoods.DataSource = dataset.Tables[0].DefaultView;
        }

        private void SetDataSource(DataSet dataset)
        {
            this.dgvGoods.DataSource = dataset.Tables[0].DefaultView;
        }

        private void ClearContentAll()
        {
            this.cmbProduct.SelectedIndex = 0;
            this.cmbType.SelectedIndex=0;
            this.txtName.Text = "";
            this.txtPrice.Text = "";
            this.txtStock.Text = "";
            this.txtDelivery.Text = "";
        }

        private void ClearCondition()
        {
            this.txtCondition.Text = "";
        }

        private void BindTypeSource()
        {
            this.cmbType.DataSource = Type.GetAllType().Tables[0].DefaultView;
            this.cmbType.DisplayMember = "TYPENAME";
            this.cmbType.ValueMember = "TYPEID";
            this.cmbProduct.DataSource = Company.GetAllCompany().Tables[0].DefaultView;
            this.cmbProduct.DisplayMember = "COMPANYNAME";
            this.cmbProduct.ValueMember = "COMPANYID";
        }
    }
}
